/*
 * @Author: LiJia
 * @Date: 2021-03-28 04:22:08
 * @LastEditTime: 2021-04-06 13:38:34
 * @LastEditors: LiJia
 * @Description: Description
 * @FilePath: \class-manage-system\miniprogram\pages\message\children\message-detail\message-detail.ts
 */
const app = getApp<IAppOption>()
Page({

  // 页面的初始数据
  data: {
    messageItem: {},
    messageRecords: [],
    keyboardHeight: 0,
    messageTemp: ""
  },

  // 生命周期函数--监听页面加载
  onLoad() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('messageItem', (req) => {
      console.log(req.data)
      this.setData({
        messageItem: req.data
      })
    })
    this.getMessageRecord()
  },

  // 生命周期函数--监听页面初次渲染完成
  onReady() {},

  // 生命周期函数--监听页面显示
  onShow() {},

  // 生命周期函数--监听页面隐藏
  onHide() {},

  // 生命周期函数--监听页面卸载
  onUnload() {},

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh() {},

  // 页面上拉触底事件的处理函数
  onReachBottom() {},

  // 用户点击右上角分享
  onShareAppMessage() {
    return {};
  },

  // ==================== 用户自定义方法 start =================== //
  getMessageRecord() {
    let record1 = [
      {
        id: '9526',
        message: '昨天发生了一件很不愉快的事情',
        time: '2021-03-07 19:08:55'
      },
      {
        id: '9527',
        message: '真的吗，是什么事情',
        time: '2021-03-07 19:09:43'
      },
      {
        id: '9526',
        message: '说来话长，等我明天去学校再跟你说吧',
        time: '2021-03-07 19:11:22'
      }
    ]
    let _messageRecords = [...this.data.messageRecords, ...record1]
    _messageRecords.forEach(_item => {
      (_item as any).isMine = (_item.id === "9527")
      console.log(app.globalData.clsUserInfo)
    })
    this.setData({
      messageRecords: _messageRecords as never,
    })
  },
  /**
   * 加载更多
   */
  handleLoadMore() {
    let record2 = [
      {
        id: '9526',
        message: '你去吃饭了吗？',
        time: '2021-03-05 11:42:06'
      },
      {
        id: '9527',
        message: '还没有，等回去',
        time: '2021-03-05 11:44:17'
      },
      {
        id: '9527',
        message: '会',
        time: '2021-03-06 11:44:34'
      },
      {
        id: '9526',
        message: '好的，我再操场这边等你',
        time: '2021-03-06 11:44:52'
      }
    ]
    let _messageRecords = [...record2, ...this.data.messageRecords]
    _messageRecords.forEach(_item => {
      (_item as any).isMine = (_item.id === "9527")
      console.log(app.globalData.clsUserInfo)
    })
    this.setData({
      messageRecords: _messageRecords as never,
    })
  },
  keyboardHeightChange(e: any) {
    console.log(e)
    this.setData({
      keyboardHeight: e.detail.height
    })
  },
  handleSendMessage() {
    console.log(this.data.messageTemp)
    if (this.data.messageTemp !== "") {
      let record = {
        id: '9527',
        message: this.data.messageTemp,
        time: (new Date()).toISOString(),
        isMine: true
      }
      let _messageRecords = [...this.data.messageRecords, record]
      debugger
      this.setData({
        messageTemp: "",
        messageRecords: _messageRecords as never
      })
    } else {
      wx.showToast({
        title: '发送消息不能为空',
        icon: 'none',
        duration: 1000
      })
    }
  },
  inputChange(e: any) {
    this.setData({
      messageTemp: e.detail.value
    })
  }
  // ==================== 用户自定义方法  end  =================== //
})

export default {}
